package com.qf.test.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.test.mapper.DtsCategoryMapper;
import com.qf.test.pojo.DtsCategory;
import com.qf.test.vo.CatVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private DtsCategoryMapper dtsCategoryMapper;


    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    //子账户账号
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    //子账户密码
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    //bucket桶空间名字
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Override
    public IPage<DtsCategory> querycategory(Integer page, Integer limit, String id, String name, String sort, String order) {

        //获取分页对象
        Page<DtsCategory> iPage = new Page<>(page, limit);
        //封装查询对象
        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);
        //创建查询条件
        if (!StringUtils.isEmpty(id)) {
            queryWrapper.eq("id", id);
        }
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.eq("name", name);
        }
        //排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            //判断是否为升序
            if (order.equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(sort);
            } else {
                //降序
                queryWrapper.orderByDesc(sort);
            }
        }
        Page<DtsCategory> page1 = dtsCategoryMapper.selectPage(iPage, queryWrapper);
        return page1;
    }

    /**
     * 添加
     */

    @Override
    public DtsCategory add(DtsCategory dtsCategory) {
        dtsCategory.setDeleted(false);
        dtsCategory.setAddTime(new Date());
        dtsCategory.setUpdateTime(new Date());
        dtsCategoryMapper.insert(dtsCategory);
        return dtsCategory;
    }

    /**
     * 修改
     *
     * @param dtsCategory
     * @return
     */
    @Override
    public int upadate(DtsCategory dtsCategory) {
        dtsCategory.setAddTime(new Date());

        int count = dtsCategoryMapper.updateById(dtsCategory);
        return count;
    }

    /**
     * 删除
     * @param dtsCategory
     * @return
     */
    @Override
    public int deletde(DtsCategory dtsCategory) {
        dtsCategory.setUpdateTime(new Date());
        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dlleted", false);
        int count = dtsCategoryMapper.update(dtsCategory, queryWrapper);
        return count;
    }

    @Override
    public List<CatVo> select() {
        List<CatVo> list = dtsCategoryMapper.findall();
        return list;
    }


}